package com.walter.controller;

import com.walter.Application;
import com.walter.annotation.SysLog;
import com.walter.common.PageVO;
import com.walter.common.Result;
import com.walter.entity.Dict;
import com.walter.model.form.SaveDictForm;
import com.walter.model.query.DictQuery;
import com.walter.service.DictService;
import com.walter.util.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：ch
 * @date ：Created in 2021/5/25
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("/api/dict")
@Api(tags = "字典管理")
public class DictController {
    @Resource
    private DictService dictService;

    @PostMapping("find")
    @ResponseBody
    @ApiOperation("后管 分页查询字典量") //可按父级字典筛选,英文名称、中文名称模糊查询
    public Result<PageVO<Dict>> find(@RequestBody DictQuery query){
        return Result.success(dictService.find(query));
    }

    @GetMapping("findAllFirstLevelDict")
    @ResponseBody
    @ApiOperation("后管 查询所有一级字典量") //分页查询的条件、保存时所属父级字典
    public Result<List<cn.hutool.core.lang.Dict>> findAllFirstLevelDict(){
        return Result.success(dictService.findAllFirstLevelDict());
    }

    @PostMapping("save")
    @ResponseBody
    @ApiOperation("后管 添加/修改字典量")
    @SysLog("后管 添加/修改字典量")
    public Result<Integer> save(@RequestBody SaveDictForm form){
        ValidationUtils.validateEntity(form); // hibernate-validator校验
        return Result.success(dictService.save(form));
    }

    @GetMapping("findByFirstLevelDict")
    @ResponseBody
    @ApiOperation("后管 根据一级字典code查询二级字典列表")
    public List<cn.hutool.core.lang.Dict> findByFirstLevelDict(String firstLevelDict){
        return Application.secondLevelDictByFirst.get(firstLevelDict);
    }

    @GetMapping("findDetail")
    @ResponseBody
    @ApiOperation("后管 查询单个详情")
    public Result<Dict> findDetail(@RequestParam Integer dictId){
        return Result.success(dictService.findDetail(dictId));
    }
}
